package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentExample;
import com.bnz.service.StudentService;
import com.bnz.util.MybatisUtil;
import com.bnz.vo.StudentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentServiceImpl implements StudentService {
	private StudentMapper studentMapper;
	private ClassesMapper classesMapper;
	//1. 查询所有学生
	@Override
	public List<Student> findAll() {
		try(SqlSession session = MybatisUtil.getSqlSession()) {
			//1.1 得到Mapper对象
			studentMapper = session.getMapper(StudentMapper.class);
			classesMapper = session.getMapper(ClassesMapper.class);
			//1.2 进行实例查询
			List<Student> students = studentMapper.selectByExample(null);
			//1.3 绑定班级名称
			for (Student student : students) {
				student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
			}
			//1.4 返回
			return students;
		}
	}

	//2. 条件查询带分页
	@Override
	public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
		//2.1 开始分页
		PageHelper.startPage(page,pagesize);
		//2.2 开始查询
		try(SqlSession session = MybatisUtil.getSqlSession()) {
			//2.2.1 得到Mapper对象
			studentMapper = session.getMapper(StudentMapper.class);
			classesMapper = session.getMapper(ClassesMapper.class);
			//2.2.2 定义条件查询实例对象
			StudentExample example = new StudentExample();
			//2.2.3 得到关联的条件对象
			StudentExample.Criteria criteria = example.createCriteria();
			//2.2.4 封装查询条件
			if(StringUtils.isNotBlank(vo.getSname())) {
				criteria.andSnameLike("%" + vo.getSname() + "%");
			}
			if(StringUtils.isNotBlank(vo.getAddr())) {
				criteria.andAddrLike("%" + vo.getAddr() + "%");
			}
			if(vo.getCid() != 0){
				criteria.andCidEqualTo(vo.getCid());
			}
			//2.2.5 开始查询
			List<Student> students = studentMapper.selectByExample(example);
			//2.2.6 绑定班级名称
			for (Student student : students) {
				student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
			}
			//2.2.7 封装成PageInfo
			PageInfo<Student> pageInfo = new PageInfo<>(students);
			//2.2.8 返回自定义分页对象
			return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
		}
	}

	//3. 添加学生
	@Override
	public void add(Student student) {
		try(SqlSession session = MybatisUtil.getSqlSession()) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.insert(student);
		}
	}

	//4. 修改学生
	@Override
	public void update(Student student) {
		try(SqlSession session = MybatisUtil.getSqlSession()) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.updateByPrimaryKey(student);
		}
	}

	//5. 删除学生
	@Override
	public void delete(String[] sids) {
		try(SqlSession session = MybatisUtil.getSqlSession()) {
			//5.1 得到mapper
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			//5.2 构造查询实例
			StudentExample example = new StudentExample();
			StudentExample.Criteria criteria = example.createCriteria();
			// List<Integer> collect = Arrays.stream(sids).map(m -> new Integer(m)).collect(Collectors.toList());
			//5.3 使用in子查询删除多个学生
			List<Integer> list = new ArrayList<>();
			for (String sid : sids) {
				list.add(new Integer(sid));
			}
			criteria.andSidIn(list);
			//5.4 根据实列删除
			mapper.deleteByExample(example);
		}
	}
}
